#include "StdAfx.h"
#include "StringDic.h"
#include <malloc.h>
CStringDic::CStringDic(void):
m_pBuff(NULL),m_nBuffSize(0),m_nBaseOffset(0)
{
}


CStringDic::~CStringDic(void)
{
}

const char* CStringDic::GetString( unsigned int nIndex,char * pBuff )
{
	nIndex -= m_nBaseOffset;
	if(m_pBuff == NULL || nIndex>=m_nBuffSize)
		return NULL;
	unsigned int uLen = (unsigned int)m_pBuff[nIndex-1];
	if( !uLen )
	{
		unsigned int uEndPos = nIndex;
		while(m_pBuff + (uEndPos++) != '\0' && uEndPos < m_nBuffSize);
		uLen = uEndPos - nIndex;
	}
	if(uLen && pBuff)
	{
		memcpy_s(pBuff,uLen,m_pBuff + nIndex ,uLen);
		pBuff[uLen] = '\0';
	}
	return m_pBuff + nIndex;
}

void CStringDic::Release()
{
	if( m_pBuff )
	{
		free(m_pBuff);
		m_pBuff = NULL;
		m_nBuffSize = 0;
	}
}

bool CStringDic::Init( const char* pBuff,size_t nBuffSize,unsigned int BaseOffset )
{
	if (pBuff)
	{
		m_pBuff = (char*)malloc(nBuffSize);
		if( !m_pBuff )
			return false;
		memcpy_s(m_pBuff,nBuffSize,pBuff,nBuffSize);
		m_nBuffSize = nBuffSize;

		m_nBaseOffset = BaseOffset;
		return true;
	}
	return false;

}
